﻿<template>
  <a-modal
    title="编辑策略"
    :width="900"
    :visible="visible"
    :confirmLoading="confirmLoading"
    @ok="handleSubmit"
    @cancel="handleCancel">
    <a-spin :spinning="confirmLoading">
      <a-form :form="form">
        <a-form-item label="安全级别" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <a-select style="width: 100%" placeholder="请选择安全级别" v-decorator="['securityLevel', {rules: [{ required: true, message: '请选择安全级别！' }]}]">
            <a-select-option v-for="(item,index) in securityLevelData" :key="index" :value="item.code">{{ item.name }}</a-select-option>
          </a-select>
        </a-form-item>
        <a-form-item label="策略名称" :labelCol="labelCol" :wrapperCol="wrapperCol" has-feedback>
          <a-input placeholder="请输入策略名称" v-decorator="['policyName', {rules: [{required: true, message: '请输入策略名称！'}]}]" />
        </a-form-item>
        <a-form-item label="摘要描述" :labelCol="labelCol" :wrapperCol="wrapperCol" has-feedback>
          <a-input placeholder="请输入摘要描述" v-decorator="['description']" />
        </a-form-item>
        <a-form-item v-show="false"><a-input v-decorator="['id']" /></a-form-item>
      </a-form>
    </a-spin>
  </a-modal>
</template>

<script>
  import {
    PolicyEdit
  } from '@/api/modular/main/PolicyManage'
  export default {
    data () {
      return {
        labelCol: {
          xs: { span: 24 },
          sm: { span: 5 }
        },
        wrapperCol: {
          xs: { span: 24 },
          sm: { span: 15 }
        },
        record: {},
        securityLevelData: [],
        visible: false,
        confirmLoading: false,
        form: this.$form.createForm(this)
      }
    },
    methods: {
	// 初始化方法
	edit (record) {
	this.visible = true
	this.record = record
	const securityLevelOption = this.$options
	this.securityLevelData = securityLevelOption.filters['enumData']('SecurityLevel')
	setTimeout(() => {
	this.form.setFieldsValue(
	{
	id: record.id,
	securityLevel: record.securityLevel,
	policyName: record.policyName,
	description: record.description
	}
	)
	}, 100)
	},
	handleSubmit () {
	const { form: { validateFields } } = this
	this.confirmLoading = true
	validateFields((errors, values) => {
	if (!errors) {
	for (const key in values) {
	// WARN: 有可能导致类型错误，注意
	if ((values[key]) !== null && typeof (values[key]) === 'object') {
	values[key] = JSON.stringify(values[key])
	this.record[key] = values[key]
	} else {
	this.record[key] = values[key]
	}
	}
	PolicyEdit(this.record).then((res) => {
	if (res.success) {
	this.$message.success('编辑成功')
	this.confirmLoading = false
	this.$emit('ok', this.record)
	this.handleCancel()
	} else {
	this.$message.error('编辑失败：' + JSON.stringify(res.message))
	}
	}).finally((res) => {
	this.confirmLoading = false
	})
	} else {
	this.confirmLoading = false
	}
	})
	},
	handleCancel () {
	this.form.resetFields()
	this.visible = false
	}
	}
	}
</script>
